<?php

namespace app\goods\admin;

use app\admin\controller\Admin;
use app\category\admin\Index as category;
use app\specification\admin\Specification as specification;
use app\goods\model\Goods as GoodsModel;
use app\common\builder\ZBuilder;

class Index extends Admin
{
    private $GoodsModel;
    private $projectid;

    public function _initialize()
    {
        parent::_initialize();
        $this->projectid = 66;
        defined('UID') or define('UID', is_signin());
        $this->GoodsModel = new GoodsModel();
    }

    /**
     * 商品管理入口
     * @return mixed
     *
     */
    public function index()
    {
        // 获取查询条件
        $map = $this->getMap();
        $map['projectid'] = $this->projectid;
        $map['isdel'] = 2;
        // 数据列表
        $data_list = $this->GoodsModel->where($map)->order('number_orders asc')->paginate();
        // 分页数据
        $page = $data_list->render();
        return ZBuilder::make('table')
            ->setPageTitle('商品管理')// 设置页面标题
            ->setTableName('goods_goods')// 设置数据表名
            ->setSearch(['id' => 'ID', 'name' => '商品名'])// 设置搜索参数
            ->addColumns([ // 批量添加列
                ['__INDEX__', '序号'],
                ['id', 'ID'],
                ['name', '商品名'],
                ['pic', '商品图', 'picture'],
                ['stores', '库存'],
                ['type', '类型', 'status', '', [1 => '实体', 2 => '虚拟', 3 => '服务']],
                ['createtime', '创建时间', ''],
                ['status', '状态', 'status', '',[1 => '上架', 2 => '下架']],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add,delete')
            ->addRightButtons('edit,delete')// 批量添加右侧按钮
            ->setRowList($data_list)// 设置表格数据
            ->fetch(); // 渲染页面
    }

    /**
     * 添加商品
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['projectid'] = $this->projectid;
            $data['createtime']=date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']);
            $result = $this->validate($data, 'Form');
            if (true !== $result) $this->error($result);
            if ($user = $this->GoodsModel->create($data)) {
                $this->success('新增成功', url('index'));
            } else {
                $this->error('新增失败');
            }
        }


        $categoryModel=new category();
        $options=$categoryModel->get_categorys();
        return ZBuilder::make('form')
            ->setPageTitle('添加商品')// 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['text', 'name', '商品名称', '必填，商品名称'],
                ['image', 'pic', '商品首图'],
                ['select', 'categoryid', '商品分类', '', $options,0],
                ['text', 'characteristic', '商品特色，卖点'],
                ['number', 'stores', '商品库存'],
                ['number', 'originalPrice', '商品原价'],
                ['text', 'minPrice', '当前价格'],
                ['select', 'expressid', '物流模板编号'],
                ['select', 'specification_type_id', '规格分类'],
                ['Images', 'pics', '商品图片'],
                ['Number', 'number_orders', '商品排序'],
                ['Ueditor', 'content', '商品详细介绍'],
                ['radio', 'type', '商品类型', '', [1 => '实体', 2 => '虚拟', 3 => '服务'], 1],
                ['radio', 'status', '状态', '', [1 => '上架', 2 => '下架'], 1]
            ])
            ->fetch();
        //return parent::add(); // TODO: Change the autogenerated stub
    }

    /**
     * 编辑商品
     * @param null $id
     * @return mixed
     */
    public function edit($id = null)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['updatetime']=date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']);//更新时间
            $where['projectid'] = $data['projectid'] = $this->projectid;
            $where['id'] = $data['id'];
            $result = $this->validate($data, 'Form');
            if (true !== $result) $this->error($result);
            if ($this->GoodsModel->update($data, $where)) {
                $this->success('修改成功', url('index'));
            } else {
                $this->error('修改失败');
            }
        }
        $where['id'] = $id;
        $where['projectid'] = $this->projectid;
        $info = $this->GoodsModel->where($where)->find();
        $categoryModel=new category();
        $options=$categoryModel->get_categorys();//分类
        $specification=new specification();//规格
        $a=$specification->returnSpecification('66');
        foreach ($a as $v){
            $specification_type[$v['id']]=$v['name'];
        }

        return ZBuilder::make('form')
            ->setPageTitle('编辑商品')// 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['text', 'name', '商品名称', '必填，商品名称'],
                ['text', 'id', '商品id', '','','','','hidden'],
                ['image', 'pic', '商品首图'],
                ['select', 'categoryid', '商品分类', '', $options],
                ['text', 'characteristic', '商品特色，卖点','中间用 ，隔开'],
                ['number', 'stores', '商品库存'],
                ['number', 'originalPrice', '商品原价'],
                ['text', 'minPrice', '当前价格'],
                ['select', 'expressid', '物流模板编号'],
                ['select', 'specification_type_id', '规格分类','',$specification_type],
                ['Images', 'pics', '商品图片'],
                ['Number', 'number_orders', '商品排序'],
                ['Ueditor', 'content', '商品详细介绍'],
                ['radio', 'type', '商品类型', '', [1 => '实体', 2 => '虚拟', 3 => '服务'], 1],
                ['radio', 'status', '状态', '', [1 => '上架', 2 => '下架'], 1],
            ])
            ->setFormData($info)
            ->fetch();
    }

    public function delete($record = [])
    {
        $param = $this->request->param();
        $ids = $param['ids'];
        $where['id'] = $ids;
        if (is_array($ids)) {
            $where['id'] = array('in', $ids);
        }
        $where['projectid'] = $this->projectid;
        $data['isdel'] = 1;
        if ($this->GoodsModel->update($data, $where)) {
            //action_log('user_add', 'admin_user', $user['id'], UID);
            $this->success('修改成功', url('index'));
        } else {
            $this->error('修改失败');
        }
    }
}